package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.AlipayMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.AlipayMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.AlipayMchntInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntAddrInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntContactInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntSettleInfoBean;
import cc.rengu.igas.bomp.facade.request.InstMchntQueryRequest;
import cc.rengu.igas.bomp.facade.response.InstMchntQueryResponse;
import cc.rengu.igas.channel.alipay.facade.bean.AddressInfo;
import cc.rengu.igas.channel.alipay.facade.bean.BankCardInfo;
import cc.rengu.igas.channel.alipay.facade.bean.ContactInfo;
import cc.rengu.igas.channel.alipay.facade.request.AliQueryMerchantRequest;
import cc.rengu.igas.channel.alipay.facade.response.AliQueryMerchantResponse;
import cc.rengu.igas.channel.wechat.facade.bean.MchntInfoBean;
import cc.rengu.igas.channel.wechat.facade.request.WechatQueryMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatQueryMerchantResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 第三方机构商户入驻查询
 * Created by moxm on 2020/07/03
 */
public class InstMchntQueryService extends ManageService {
    /*微信支付通道*/
    private final String wxChannelFlag = "01";
    /*支付宝支付通道*/
    private final String zfbChannelFlag = "02";

    @Override
    protected Object bizBeforeCust() throws Exception {
        InstMchntQueryRequest instMchntQueryRequest = new InstMchntQueryRequest();
        ConvertUtil.convertOutput(instMchntQueryRequest);
        return instMchntQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        InstMchntQueryRequest instMchntQueryRequest = (InstMchntQueryRequest) request;
        InstMchntQueryResponse instMchntQueryResponse = new InstMchntQueryResponse();

        /* 法人行标识 */
        String instId = instMchntQueryRequest.getHeader().getInstId();
        /*用户标识*/
        String userId = instMchntQueryRequest.getUserId();
        /*商户号*/
        String mchntNo = instMchntQueryRequest.getMchntNo();
        /*第三方通道标识*/
        String channelFlag = instMchntQueryRequest.getChannelFlag();
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(channelFlag)) {
            rglog.error("必填性校验失败");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList();
        if (wxChannelFlag.equals(channelFlag)) {
            List<ChannelCallCfg> msgDstchannelCallCfgList = channelCallCfgList.stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPWX.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUWX.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.WXZF.getChannelType())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(msgDstchannelCallCfgList)) {
                rglog.error("微信通道交易未配置！");
                throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
            }
            ChannelCallCfg channelCallCfg = msgDstchannelCallCfgList.get(0);
            instMchntQueryResponse = wechatMchntQuery(instId, mchntNo, channelCallCfg);
        } else if (zfbChannelFlag.equals(channelFlag)) {
            List<ChannelCallCfg> msgDstchannelCallCfgList = channelCallCfgList.stream().filter(item -> item.getCallChannelId().equals(ChannelEnum.UPAL.getChannelType()) ||
                    item.getCallChannelId().equals(ChannelEnum.NUAL.getChannelType()) || item.getCallChannelId().equals(ChannelEnum.ALIP.getChannelType())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(msgDstchannelCallCfgList)) {
                rglog.error("支付宝通道交易未配置！");
                throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
            }
            ChannelCallCfg channelCallCfg = msgDstchannelCallCfgList.get(0);
            instMchntQueryResponse = aliPayMchntQuery(instId, mchntNo, channelCallCfg);
        } else {
            rglog.error("入参检查失败!");
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }
        /* 服务响应报文组装 */
        BizResponse<InstMchntQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(instMchntQueryResponse.getRespCode());
        bizResponse.setRespDesc(instMchntQueryResponse.getRespDesc());
        bizResponse.setResult(instMchntQueryResponse);
        return bizResponse;
    }

    /**
     * 微信商户信息查询
     *
     * @param instId
     * @param mchntNo        商户号
     * @param channelCallCfg 渠道与通道通道服务调用配置
     * @throws Exception 异常
     */
    private InstMchntQueryResponse wechatMchntQuery(String instId, String mchntNo, ChannelCallCfg channelCallCfg) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        InstMchntQueryResponse instMchntQueryResponse = new InstMchntQueryResponse();

        /* 获取微信商户号 */
        String wechatMchntNo = "";
        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(instId, mchntNo);
        if (null == wechatMchntInfo || StringUtil.isEmptyOrNull(wechatMchntInfo.getDstChannelType())) {
            rglog.error("微信商户信息查询失败");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        } else {
            wechatMchntNo = wechatMchntInfo.getWechatMchntNo();
        }

        /* 获取通道标识 */
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(wechatMchntInfo.getInstId(), BompAppParamConstant.SYS_PARAM_CHANNEL, BompAppParamConstant.WECHAT_PAY_CHANNEL_ID);
        if (null == sysParam) {
            rglog.error("机构<{}>未配置支付宝支付通道选择", wechatMchntInfo.getInstId());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        /* 通过dubbo服务调用通道方法查询微信商户信息 */
        DubboService dubboService = new DubboServiceImpl();
        /* 请求信息处理 */
        WechatQueryMerchantRequest wechatQueryMerchantRequest = new WechatQueryMerchantRequest();
        /*报文头*/
        cc.rengu.igas.channel.wechat.facade.base.Header header = new cc.rengu.igas.channel.wechat.facade.base.Header();
        header.setInstId(instId);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setTraceNo(sysSeqNum + channelCallCfg.getCallIndex());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(BompAppParamConstant.BOMP_SYS_ID);
        header.setBizType(channelCallCfg.getBizType());
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        wechatQueryMerchantRequest.setHeader(header);
        /*通道标识赋值*/
        wechatQueryMerchantRequest.setChannelId(sysParam.getParamValue());
        /*通道类型赋值*/
        wechatQueryMerchantRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        /*商户号赋值*/
        wechatQueryMerchantRequest.setMchntNo(mchntNo);
        /*微信商户号赋值*/
        wechatQueryMerchantRequest.setThirdMchntNo(wechatMchntNo);
        cc.rengu.igas.channel.wechat.facade.result.Result<WechatQueryMerchantResponse> result =
                (cc.rengu.igas.channel.wechat.facade.result.Result<WechatQueryMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "queryMerchant", wechatQueryMerchantRequest);
        WechatQueryMerchantResponse wechatQueryMerchantResponse = result.getResult();
        if (!result.isSuccess() || !BompAppParamConstant.DUBBO_SUCCESS.equals(wechatQueryMerchantResponse.getRespCode())) {
            rglog.error("微信商户查询(调用duboo)处理失败，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            // 更新微信入驻标识为失败
            if (StringUtil.isEmptyOrNull(wechatMchntNo)) {
                //入驻失败
                wechatMchntInfo.setWechatEntryFlag("02");
                int updateResult = wechatMchntInfoMapper.updateWechatMchntInfoNoTransactionById(wechatMchntInfo);
                if (0 != updateResult) {
                    rglog.error("更新微信商户信息失败");
                    throw new BizException(RespCodeEnum.UPDATE_WECHAT_MCHNT_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_WECHAT_MCHNT_INFO_ERROR.getRespDesc());
                }
                instMchntQueryResponse.setRespCode(RespCodeEnum.CREATE_WECHAT_MACHNT_INFO_ERROR.getRespCode());
                instMchntQueryResponse.setRespDesc(RespCodeEnum.CREATE_WECHAT_MACHNT_INFO_ERROR.getRespDesc());
            }

        } else {
            if (!CollectionUtils.isEmpty(wechatQueryMerchantResponse.getMchntInfoBeanList())) {
                MchntInfoBean mchntInfoBean = wechatQueryMerchantResponse.getMchntInfoBeanList().get(0);
                /*商户号*/
                instMchntQueryResponse.setMchntNo(mchntNo);
                /*第三方商户号*/
                instMchntQueryResponse.setThirdMchntNo(mchntInfoBean.getThirdMchntNo());
                /*商户名称*/
                instMchntQueryResponse.setMchntName(mchntInfoBean.getMchntName());
                /*商户简称*/
                instMchntQueryResponse.setMchntShortName(mchntInfoBean.getMchntAbbr());
                /*客服电话*/
                instMchntQueryResponse.setServicePhone(mchntInfoBean.getServicePhone());
                /*经营类目*/
                instMchntQueryResponse.setBusiness(mchntInfoBean.getBusiness());
                /* 1-商户联系人信息列表 */
                List<MchntContactInfoBean> alipayMchntContactInfoList = new ArrayList<>();
                MchntContactInfoBean alipayMchntContactInfoBean = new MchntContactInfoBean();
                alipayMchntContactInfoBean.setContactName(mchntInfoBean.getContact());
                alipayMchntContactInfoBean.setContactPhone(mchntInfoBean.getServicePhone());
                alipayMchntContactInfoBean.setContactEmail(mchntInfoBean.getContactEmail());
                alipayMchntContactInfoList.add(alipayMchntContactInfoBean);
                instMchntQueryResponse.setMchntContactInfoList(alipayMchntContactInfoList);

                // 更新微信入驻标识为失败
                if (StringUtil.isEmptyOrNull(wechatMchntNo)) {
                    //入驻成功
                    wechatMchntInfo.setWechatMchntNo(mchntInfoBean.getThirdMchntNo());
                    wechatMchntInfo.setWechatEntryFlag("01");
                    wechatMchntInfo.setLastEntryTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                    int updateResult = wechatMchntInfoMapper.updateWechatMchntInfoNoTransactionById(wechatMchntInfo);
                    if (0 != updateResult) {
                        rglog.error("更新微信商户信息失败");
                        throw new BizException(RespCodeEnum.UPDATE_WECHAT_MCHNT_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_WECHAT_MCHNT_INFO_ERROR.getRespDesc());
                    }
                }

                instMchntQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                instMchntQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            } else {
                rglog.error("微信商户查询(调用duboo)处理失败，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
                // 更新微信入驻标识为失败
                if (StringUtil.isEmptyOrNull(wechatMchntNo)) {
                    //入驻失败
                    wechatMchntInfo.setWechatEntryFlag("02");
                    int updateResult = wechatMchntInfoMapper.updateWechatMchntInfoNoTransactionById(wechatMchntInfo);
                    if (0 != updateResult) {
                        rglog.error("更新微信商户信息失败");
                        throw new BizException(RespCodeEnum.UPDATE_WECHAT_MCHNT_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_WECHAT_MCHNT_INFO_ERROR.getRespDesc());
                    }
                    instMchntQueryResponse.setRespCode(RespCodeEnum.CREATE_WECHAT_MACHNT_INFO_ERROR.getRespCode());
                    instMchntQueryResponse.setRespDesc(RespCodeEnum.CREATE_WECHAT_MACHNT_INFO_ERROR.getRespDesc());
                }
            }
        }
        return instMchntQueryResponse;
    }

    /**
     * 支付宝商户信息查询
     *
     * @param instId
     * @param mchntNo        商户号
     * @param channelCallCfg 渠道与通道通道服务调用配置
     * @throws Exception 异常
     */
    private InstMchntQueryResponse aliPayMchntQuery(String instId, String mchntNo, ChannelCallCfg channelCallCfg) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        InstMchntQueryResponse instMchntQueryResponse = new InstMchntQueryResponse();

        /* 获取支付宝商户号 */
        String aliMchntNo;
        AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
        AlipayMchntInfo alipayMchntInfo = alipayMchntInfoMapper.selectAlipayMchntInfoByPrimaryKey(instId, mchntNo);
        if (null == alipayMchntInfo || StringUtil.isEmptyOrNull(alipayMchntInfo.getDstChannelType())) {
            rglog.error("支付宝商户信息查询失败，或支付宝费率通道为空！");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        } else {
            aliMchntNo = alipayMchntInfo.getAlipayMchntNo();
        }

        /* 通过dubbo服务调用通道方法查询支付宝商户信息 */
        DubboService dubboService = new DubboServiceImpl();
        /* 请求信息处理 */
        AliQueryMerchantRequest aliQueryMerchantRequest = new AliQueryMerchantRequest();
        /*报文头*/
        cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
        header.setInstId(instId);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setTraceNo(sysSeqNum + channelCallCfg.getCallIndex());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(BompAppParamConstant.BOMP_SYS_ID);
        header.setBizType(channelCallCfg.getBizType());
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        aliQueryMerchantRequest.setHeader(header);
        /*通道标识赋值*/
        aliQueryMerchantRequest.setChannelId(channelCallCfg.getCallChannelId());
        /*通道类型赋值*/
        aliQueryMerchantRequest.setChannelType(alipayMchntInfo.getDstChannelType());
        /*应用授权令牌*/
        aliQueryMerchantRequest.setAppAuthToken(alipayMchntInfo.getAppAuthToken());
        /*商户号赋值*/
        aliQueryMerchantRequest.setMchntNo(mchntNo);
        /*支付宝商户号赋值*/
        aliQueryMerchantRequest.setAliMchntNo(aliMchntNo);
        /*全局唯一事务编号*/
        aliQueryMerchantRequest.setBatchNo(alipayMchntInfo.getBatchNo());
        cc.rengu.igas.channel.alipay.facade.result.Result<AliQueryMerchantResponse> result =
                (cc.rengu.igas.channel.alipay.facade.result.Result<AliQueryMerchantResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_ALIPAY_MCHNT_SERVICE, "aliQueryMerchant", aliQueryMerchantRequest);
        AliQueryMerchantResponse aliQueryMerchantResponse = result.getResult();
        if (!result.isSuccess() || !BompAppParamConstant.DUBBO_SUCCESS.equals(aliQueryMerchantResponse.getRespCode())) {
            rglog.error("支付宝商户查询(调用duboo)处理失败，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            if (StringUtil.isEmptyOrNull(aliMchntNo)) {
                //入驻失败
                alipayMchntInfo.setAlipayEntryFlag("02");
                int updateResult = alipayMchntInfoMapper.updateAlipayMchntInfoNoTransactionById(alipayMchntInfo);
                if (0 != updateResult) {
                    rglog.error("更新微信商户信息失败");
                    throw new BizException(RespCodeEnum.UPDATE_ALIMCHNT_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_ALIMCHNT_INFO_ERROR.getRespDesc());
                }
            }
            instMchntQueryResponse.setRespCode(RespCodeEnum.CREATE_ALIPAY_MACHNT_INFO_ERROR.getRespCode());
            instMchntQueryResponse.setRespDesc(RespCodeEnum.CREATE_ALIPAY_MACHNT_INFO_ERROR.getRespDesc());
        } else {
            // 更新支付宝入驻标识为失败
            if (!StringUtil.isEmptyOrNull(aliQueryMerchantResponse.getAliMchntNo())) {
                //入驻成功
                alipayMchntInfo.setAlipayMchntNo(aliQueryMerchantResponse.getAliMchntNo());
                alipayMchntInfo.setAlipayEntryFlag("01");
                alipayMchntInfo.setLastEntryTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                alipayMchntInfo.setLastUpdateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                int updateResult = alipayMchntInfoMapper.updateAlipayMchntInfoNoTransactionById(alipayMchntInfo);
                if (0 != updateResult) {
                    rglog.error("更新微信商户信息失败");
                    throw new BizException(RespCodeEnum.UPDATE_ALIMCHNT_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_ALIMCHNT_INFO_ERROR.getRespDesc());
                }
            } else {
                //入驻失败
                alipayMchntInfo.setAlipayEntryFlag("02");
                alipayMchntInfo.setLastUpdateTime(DateUtil.getCurrentDateTime(InitParamUtil.yyyy_MM_dd_HHmmssSSS));
                int updateResult = alipayMchntInfoMapper.updateAlipayMchntInfoNoTransactionById(alipayMchntInfo);
                if (0 != updateResult) {
                    rglog.error("更新微信商户信息失败");
                    throw new BizException(RespCodeEnum.UPDATE_ALIMCHNT_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_ALIMCHNT_INFO_ERROR.getRespDesc());
                }
            }

            /*商户号*/
            instMchntQueryResponse.setMchntNo(mchntNo);
            /*第三方商户号*/
            instMchntQueryResponse.setThirdMchntNo(aliMchntNo);
            /*商户名称*/
            instMchntQueryResponse.setMchntName(aliQueryMerchantResponse.getMchntName());
            /*商户简称*/
            instMchntQueryResponse.setMchntShortName(aliQueryMerchantResponse.getMchntAbbr());
            /*客服电话*/
            instMchntQueryResponse.setServicePhone(aliQueryMerchantResponse.getServicePhone());
            /*商户证件编号*/
            instMchntQueryResponse.setCertNo(aliQueryMerchantResponse.getMchntCertId());
            /*商户证件类型*/
            instMchntQueryResponse.setCertType(aliQueryMerchantResponse.getMchntCertType());
            /*商户备注信息*/
            instMchntQueryResponse.setMchntRemark(aliQueryMerchantResponse.getMchntRemark());
            /* 1-商户联系人信息列表 */
            List<MchntContactInfoBean> alipayMchntContactInfoList = new ArrayList<>();
            /* 2-商户地址信息列表 */
            List<MchntAddrInfoBean> alipayMchntAddrInfoList = new ArrayList<>();
            /* 3-商户结算卡信息列表 */
            List<MchntSettleInfoBean> alipayMchntSettleInfoList = new ArrayList<>();
            /* 1.1-商户联系人信息转换 */
            if (!CollectionUtils.isEmpty(aliQueryMerchantResponse.getContactInfoList())) {
                aliQueryMerchantResponse.getContactInfoList().forEach(item -> convContactInfo(item, alipayMchntContactInfoList));
            }
            /* 2.1-商户地址信息转换 */
            if (!CollectionUtils.isEmpty(aliQueryMerchantResponse.getAddressInfoList())) {
                aliQueryMerchantResponse.getAddressInfoList().forEach(item -> convAddressInfo(item, alipayMchntAddrInfoList));
            }
            /* 3.1-商户结算卡信息 */
            if (!CollectionUtils.isEmpty(aliQueryMerchantResponse.getBankCardInfoList())) {
                aliQueryMerchantResponse.getBankCardInfoList().forEach(item -> convBankCardInfo(item, alipayMchntSettleInfoList));
            }
            /*联系人信息赋值*/
            instMchntQueryResponse.setMchntContactInfoList(alipayMchntContactInfoList);
            /*商户地址信息赋值*/
            instMchntQueryResponse.setMchntAddrInfoList(alipayMchntAddrInfoList);
            /*商户结算信息赋值*/
            instMchntQueryResponse.setMchntSettleInfoList(alipayMchntSettleInfoList);

            instMchntQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            instMchntQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        return instMchntQueryResponse;
    }

    /* 商户联系人信息转换 */
    private void convContactInfo(ContactInfo contactInfo, List<MchntContactInfoBean> alipayMchntContactInfoList) {
        /*新建商户联系人信息对象*/
        MchntContactInfoBean alipayMchntContactInfo = new MchntContactInfoBean();
        /*联系人姓名赋值*/
        alipayMchntContactInfo.setContactName(contactInfo.getContact());
        /*联系人电话赋值*/
        alipayMchntContactInfo.setContactPhone(contactInfo.getContactPhone());
        /*联系手机赋值*/
        alipayMchntContactInfo.setContactMobile(contactInfo.getContactTelephone());
        /*业务标识赋值*/
        alipayMchntContactInfo.setBizTag(contactInfo.getContactJobType());
        /*联系人证件号赋值*/
        alipayMchntContactInfo.setContactCertNo(contactInfo.getCertId());
        /*联系人类型赋值*/
        alipayMchntContactInfo.setContactType(contactInfo.getContactType());
        /*联系人邮箱赋值*/
        alipayMchntContactInfo.setContactEmail(contactInfo.getContactEmail());
        /*添加对象*/
        alipayMchntContactInfoList.add(alipayMchntContactInfo);
    }

    /* 商户地址信息转换 */
    private void convAddressInfo(AddressInfo addressInfo, List<MchntAddrInfoBean> alipayMchntAddrInfoList) {
        /*新建商户地址信息对象*/
        MchntAddrInfoBean alipayMchntAddrInfo = new MchntAddrInfoBean();
        /* 默认类型值BUSINESS_ADDRESS */
        alipayMchntAddrInfo.setAddrType("BUSINESS_ADDRESS");
        /*商户所在省份赋值*/
        alipayMchntAddrInfo.setProvinceCode(addressInfo.getProvinceCode());
        /*商户所在城市赋值*/
        alipayMchntAddrInfo.setCityCode(addressInfo.getCityCode());
        /*商户所在区县赋值*/
        alipayMchntAddrInfo.setCountyCode(addressInfo.getDistrictCode());
        /*商户地址赋值*/
        alipayMchntAddrInfo.setLatitude(addressInfo.getLatitude());
        /*商户经度赋值*/
        alipayMchntAddrInfo.setLongitude(addressInfo.getLongitude());
        /*商户纬度赋值*/
        alipayMchntAddrInfo.setMchntAddr(addressInfo.getAddress());
        /*添加对象*/
        alipayMchntAddrInfoList.add(alipayMchntAddrInfo);
    }

    /* 商户结算卡信息转换 */
    private void convBankCardInfo(BankCardInfo bankCardInfo, List<MchntSettleInfoBean> alipayMchntSettleInfoList) {
        /* 新建商户结算信息对象*/
        MchntSettleInfoBean alipayMchntSettleInfo = new MchntSettleInfoBean();
        /*商户结算账户姓名赋值*/
        alipayMchntSettleInfo.setSettleAcctName(bankCardInfo.getAcctName());
        /*商户结算账号赋值*/
        alipayMchntSettleInfo.setSettleAcctNo(bankCardInfo.getAcctNo());
        /*添加对象*/
        alipayMchntSettleInfoList.add(alipayMchntSettleInfo);
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        InstMchntQueryResponse instMchntQueryResponse = (InstMchntQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(instMchntQueryResponse);
    }
}
